package com.yjhb.android.sbz.view;

import android.content.Context;
import android.database.DataSetObserver;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Adapter;
import android.widget.LinearLayout;

/**
 * 创建者 zx
 * 创建时间 2020/11/20 10:23
 */
public class LowListView extends LinearLayout {
    private final String TAG = "LowListView";

    private Adapter adapter;

    public LowListView(Context context) {
        super(context);
    }

    public LowListView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public LowListView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    public void setAdapter(Adapter adapter) {
        this.adapter = adapter;
        if (adapter != null) {
            adapter.registerDataSetObserver(new DataSetObserver() {
                @Override
                public void onChanged() {
                    super.onChanged();
                    updateUi();
                }
            });
            updateUi();
        }
    }


    private void updateUi(){
        removeAllViews();
        if (adapter!=null){
            for (int i = 0; i<adapter.getCount(); i++) {
                LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
                View view = adapter.getView(i, null, null);
                addView(view, params);
            }
        }
        requestLayout();
    }


}
